package com.alongstudio.account.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.alongstudio.account.commons.exception.BusinessException;
import com.alongstudio.account.commons.utils.BeanUtil;
import com.alongstudio.account.constants.SystemConstant;
import com.alongstudio.account.dao.IKeepAccountsDetailDao;
import com.alongstudio.account.entity.Account;
import com.alongstudio.account.entity.Category;
import com.alongstudio.account.entity.KeepAccountsDetail;
import com.alongstudio.account.entity.Merchant;
import com.alongstudio.account.entity.Project;
import com.alongstudio.account.service.IAccountService;
import com.alongstudio.account.service.ICategoryService;
import com.alongstudio.account.service.IKeepAccountsDetailService;
import com.alongstudio.account.service.IMerchantService;
import com.alongstudio.account.service.IProjectService;
import com.alongstudio.account.vo.KeepAccountsDetailVo;
@Service(value = "KeepAccountsDetailService")
public class KeepAccountsDetailService implements IKeepAccountsDetailService {
	@Autowired
	@Qualifier("CategoryService")
	private ICategoryService cateService;
	@Autowired
	@Qualifier("AccountService")
	private IAccountService accountService;
	@Autowired
	@Qualifier("ProjectService")
	private IProjectService projectService;
	@Autowired
	@Qualifier("MerchantService")
	private IMerchantService merchantService;
	@Autowired
	private IKeepAccountsDetailDao keepAccountsDetailDao;

	private String className = KeepAccountsDetailService.class.getName();

	@Override
	public Map findKeepAccountsInitData(String userId) throws BusinessException {
		Map result = new HashMap();
//		try {
//			List<Category> expenditureCategoryList = cateService.findFirstClassCategory(
//					userId, SystemConstant.CATEGORY_TYPE_EXPENDITURE);
//
//			if (expenditureCategoryList != null && !expenditureCategoryList.isEmpty()) {
//				result.put("expenditureCategoryList", expenditureCategoryList);
//				Category firstCate = expenditureCategoryList.get(0);
//				List<Category> subExpenditureCategoryList = cateService.findSubCategoryByParentId(firstCate.getId());
//				result.put("subExpenditureCategoryList", subExpenditureCategoryList);
//			}
//
//			List<Category> incomeCategoryList = cateService.findFirstClassCategory(userId,SystemConstant.CATEGORY_TYPE_INCOME
//					);
//
//			if (incomeCategoryList != null && !incomeCategoryList.isEmpty()) {
//				result.put("incomeCategoryList", incomeCategoryList);
//				Category firstCate = incomeCategoryList.get(0);
//				List<Category> subIncomeCategoryList = cateService.findSubCategoryByParentId(firstCate.getId());
//				result.put("subIncomeCategoryList", subIncomeCategoryList);
//			}
//
//			Map accounts = accountService.findInitAccounts(userId);
//			List<Project> projectList = projectService.findAllProject(userId);
//			List<Merchant> merchantList = merchantService.findAllMerchant(userId);
//			result.putAll(accounts);
//			result.put("projectList", projectList);
//			result.put("merchantList", merchantList);
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		return result;
	}

	@Override
	public boolean createDetail(KeepAccountsDetailVo vo) throws BusinessException {
		boolean result = false;
		try {
			KeepAccountsDetail detail = new KeepAccountsDetail();
//			detail.setId(vo.getId());
//			detail.setAmount(vo.getAmount());
//			detail.setHappenedDate(vo.getHappenedDate());
//			detail.setCreateTime(vo.getCreateTime());
//			detail.setRemark(vo.getRemark());
			BeanUtil.copyProperties(vo, detail, true);
			if(vo.getAccountId() != null){
				Account account = new Account();
				account.setId(vo.getAccountId());
				detail.setAccount(account);
			}
			if(vo.getCategoryId() != null){
				Category cat = new Category();
//				cat.setId(vo.getCategoryId());
			}
			keepAccountsDetailDao.create(detail);
			
			result = (detail.getId() != null);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(className, e);
		}
		return result;
	}
}
